/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Copyright (C) 2024-2025 Jim Gao <gaozhsoft@aliyun.com>
 *
 */
#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>

#include <linux/timer.h>
#include <linux/delay.h>

struct timer_list drv_timer;	/* timer callback for checking ack of stop request */
volatile int run_cnt = 0;
#define DRV_MSLEEP              (500)

/* timer callback for checking the ack of stop request */
static void drv_timer_callback(struct timer_list *t)
{
    printk("[%s +%d %s] run_cnt=%d\n", __FILE__, __LINE__, __func__, run_cnt);
    if (10 > run_cnt)
    {
        run_cnt ++;
        mod_timer(&drv_timer, jiffies + msecs_to_jiffies(DRV_MSLEEP));
    }
}

static int __init timer_drv_init(void)
{
    int ret = 0;
    printk("[%s +%d %s] start\n", __FILE__, __LINE__, __func__);
    
    timer_setup(&drv_timer, drv_timer_callback, 0);

    ret = mod_timer(&drv_timer, jiffies + msecs_to_jiffies(DRV_MSLEEP));
    if (ret)
    {
        printk("[%s +%d %s] mod_timer(500ms) failed\n", __FILE__, __LINE__, __func__);
    }
     else
    {
        printk("[%s +%d %s] mod_timer(500ms) success\n", __FILE__, __LINE__, __func__);
    }
    printk("[%s +%d %s] end\n", __FILE__, __LINE__, __func__);
    return 0;
}

static void __exit timer_drv_exit(void)
{
    int ret = 0;
    printk("[%s +%d %s] start\n", __FILE__, __LINE__, __func__);
    ret = del_timer(&drv_timer);
    if (ret)
    {
        printk("[%s +%d %s] del_timer(&drv_timer) failed\n", __FILE__, __LINE__, __func__);
    }
    printk("[%s +%d %s] end\n", __FILE__, __LINE__, __func__);

}

module_init(timer_drv_init);
module_exit(timer_drv_exit);

MODULE_AUTHOR("Jim Gao <gaozhsoft@aliyun.com>");
MODULE_DESCRIPTION("basic example for timer driver");
MODULE_LICENSE("GPL v2");

